package com.wang.work;

import java.util.concurrent.locks.ReentrantLock;

public class RobBeansImpl implements Runnable {
	public int sum = 200;//因为线程对象公用一个业务实现类所以不用static修饰
	public ReentrantLock lock = new ReentrantLock();
	@Override
	public void run() {
		while(sum > 0){
			rob();
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	public void rob(){
		lock.lock();
		try {
			if(sum > 0)
				System.out.println(Thread.currentThread().getName()+"抢到第"+sum--+"豆子");
		}finally{
			lock.unlock();
		}
	}
}
